package com.example.zwh.computershop.my_shopp;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.zwh.computershop.R;
import com.example.zwh.computershop.sqlModel.Address;

import java.util.ArrayList;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {
    private ArrayList<Address> addresses;
    private OnItemClickLitener mOnItemClickLitener;

    public  AddressAdapter(ArrayList<Address> addresses){
        this.addresses = addresses;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView addree_man_name;
        TextView address_telephone;
        TextView address_real;
        Button modify_address;

        public ViewHolder (View view) {
            super(view);
            addree_man_name = view.findViewById(R.id.addree_man_name);
            address_telephone = view.findViewById(R.id.address_telephone);
            address_real = view.findViewById(R.id.address_real);
            modify_address = view.findViewById(R.id.modify_address);
        }
    }

    public void update() {
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.address_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int i) {
        Address address = addresses.get(i);
        viewHolder.addree_man_name.setText(address.getName());
        viewHolder.address_telephone.setText(address.getPhone());
        viewHolder.address_real.setText(address.getAddress());
        viewHolder.modify_address.setOnClickListener(new View.OnClickListener() {
            int pos = viewHolder.getLayoutPosition();
            Address ad = addresses.get(pos);
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(ad.getAddressId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return addresses.size();
    }

    //起到传值效果
    public interface OnItemClickLitener {
        public void onItemClick(int aid);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
